

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Data_Type extends JFrame{
    public Data_Type(String typeID){


        SQLOperation sql=new SQLOperation();
        String[] deviceAttri={"idDevice","Family_familyID"};
        Object[][] deviceData=sql.search("Device",deviceAttri,"DeviceType_typeID",typeID);


        String[] dataBelongs={"Lamps","WindowsDoors","Humidity","Temperature"};
        String[][] dataInfo={{"isOn","timeStamp"},
                {"isOpen","timeStamp"},
                {"hygro","timeStamp"},
                {"tempNum","timeStamp"}};

        Object[][] example;

        int num=-1;
        int valid;
        for (int i = 0; i < 4; i++) {
            valid=-1;
            for(int j=0;j<deviceData.length;j++){
                if(!deviceData[j][1].equals("0")){
                    valid=j;
                    break;
                }
            }

            if(valid!=-1) {
                example = sql.search(dataBelongs[i], dataInfo[i], "Device_idDevice", deviceData[valid][0]);
                if (example.length != 0) {
                    num = i;
                    break;


                }
            }
        }
        if(num!=-1) {

            setTitle("Information");
            Container container = getContentPane();
            setLayout(new BorderLayout());

            JPanel titlePanel = new JPanel();

            JLabel title = new JLabel("Device Data of Type '" + typeID + "'");
            title.setFont(new Font("", Font.BOLD, 20));

            titlePanel.add(title,BorderLayout.NORTH);

            Object[][][] detailData=new Object[deviceData.length][][];

            int totalLength=0;
            String[] moreDataInfo=new String[3];
            for(int i=0;i<2;i++){
                moreDataInfo[i]=dataInfo[num][i];
            }
            moreDataInfo[2]="Device_idDevice";

            for(int i=0;i<deviceData.length;i++){

                detailData[i]=sql.search(dataBelongs[num],moreDataInfo,"Device_idDevice",deviceData[i][0]);

                totalLength+=detailData[i].length;

            }

            Object[][] gatherList=new Object[totalLength][3];

            int count=0;
            for(int i=0;i<deviceData.length;i++){
                for(int j=0;j<detailData[i].length;j++){
                    for(int k=0;k<3;k++){
                        gatherList[count][k]=detailData[i][j][k];
                    }

                    count++;
                }
            }



            JTable dataList = new JTable(gatherList, moreDataInfo);

            dataList.setRowHeight(30);
            dataList.setFont(new Font("", Font.PLAIN, 15));

            dataList.setEnabled(true);
            dataList.getTableHeader().setFont(new Font("", Font.BOLD, 20));
            JScrollPane scrollPane = new JScrollPane(dataList);

            add(scrollPane, BorderLayout.CENTER);

            JButton back = new JButton("Back");
            back.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            JComboBox selectCombo = new JComboBox();
            JButton further = new JButton("Analyze");

            if (num == 0 || num == 1) {
                selectCombo.addItem("off");
                selectCombo.addItem("on");

            } else {

                selectCombo.addItem("Less than:");
                selectCombo.addItem("More than:");


            }

            JTextField textBox = new JTextField(8);
            if (num == 0 || num == 1) {
                textBox.setEnabled(false);
            } else {
                textBox.setEnabled(true);
                textBox.setText("40");
            }




            further.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!textBox.isEnabled()){
                        new DataFinalList(gatherList,moreDataInfo, selectCombo.getSelectedIndex() == 1,0);
                    }
                    else{
                        new DataFinalList(gatherList,moreDataInfo, selectCombo.getSelectedIndex() == 1,
                                Integer.parseInt((String)textBox.getText()),0);
                    }
                }
            });


            JPanel buttons=new JPanel();
            buttons.add(back);
            buttons.add(selectCombo);
            buttons.add(textBox);

            buttons.add(further);
            add(buttons,BorderLayout.SOUTH);

            setSize(600,500);
            setResizable(false);
            setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            setVisible(true);

        }
        else{
            JOptionPane.showMessageDialog
                    (null, "Invalid typeID!","Empty select",JOptionPane.ERROR_MESSAGE);
            setVisible(false);


        }







    }
}